package com.chianghao.core.frame.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.chianghao.core.lambda.IGetter;
import com.chianghao.core.mybatis.mapper.BaseMapper;
import com.chianghao.core.mybatis.plugins.Paging;
import com.chianghao.core.mybatis.sql.SqlOrder;
import com.chianghao.core.mybatis.sql.SqlWrapper;
import com.chianghao.core.mybatis.sql.db_enum.SqlOrderType;

public abstract class BaseService<M extends BaseMapper<T>,T> {
	
	@Autowired
	protected M mapper;

	public M getMapper() {
		return mapper;
	}
	
	public void setMapper(M mapper) {
		this.mapper = mapper;
	}
	
	
	/**
	 * 插入必须要Entity中主键是code ，且非空
	 * @param bean
	 */
	@Transactional(rollbackFor=Exception.class)
	public void insert(T bean) {
		getMapper().insert(bean);
	}
	
	/**
	 * 批量插入
	 * @param beans
	 */
	@Transactional(rollbackFor=Exception.class)
	public void insertBatch(List<T> beans) {
		getMapper().batchInsert(beans);
	}
	
	
	
	
	
	/**
	 * 查询fieldName = value的数量
	 * @param fieldName
	 * @param value
	 * @return
	 */
	protected int queryCount(IGetter<T> fn,Object value) {
		SqlWrapper sqlWrapper = new SqlWrapper();
		sqlWrapper.addCondition(fn, value);
		int count = this.mapper.count(sqlWrapper);
		return count;
	}
	
	/**
	 * 查询主键的个数
	 * @param code
	 * @return
	 */
	protected boolean isExist(IGetter<T> fn,Object code) {
		SqlWrapper sqlWrapper = new SqlWrapper();
		sqlWrapper.addCondition(fn, code);
		Integer count = this.mapper.count(sqlWrapper);
		return count>0?true:false;
	}
	
	/**
	 * 查询list
	 * @param where
	 * @return
	 */
	public List<T> query(T where) {
		return getMapper().queryListByEntity(where, null);
	}
	/**
	 * 查询list
	 * @param where
	 * @return
	 */
	protected List<T> query(T where,String orderFiledName,SqlOrderType orderType) {
		SqlOrder order  = new SqlOrder();
		order.add(orderFiledName, orderType);
		return getMapper().queryListByEntity(where, order);
	}
	
	/**
	 * 查询分页list
	 * @param where
	 * @param paging
	 * @return
	 */
	public List<T> queryPage(T where,Paging paging){
		return getMapper().queryPageListByEntity(paging, where, null);
	}
	
	/**
	 * 查询分页list
	 * @param where
	 * @param paging
	 * @return
	 */
	protected List<T> queryPage(T where,Paging paging,String orderFiledName,SqlOrderType orderType){
		SqlOrder order  = new SqlOrder();
		order.add(orderFiledName, orderType);
		return getMapper().queryPageListByEntity(paging, where, order);
	}
	
	/**
	 * 根据传入的实体查新
	 * @param where
	 * @return
	 */
	public T queryBean(T where) {
		return getMapper().queryOneByEntity(where);
	}
	
	@Transactional(rollbackFor=Exception.class)
	public void deleteById(Object id) {
		this.getMapper().delete(new SqlWrapper().addCondition("id",id));
	}
	
	
	@Transactional(rollbackFor=Exception.class)
	public void deleteByCode(Object code) {
		this.getMapper().delete(new SqlWrapper().addCondition("code",code));
	}
	
	
	@Transactional(rollbackFor=Exception.class)
	public void updateById(T bean,Object id) {
		this.getMapper().update(bean, new SqlWrapper().addCondition("id",id));
	}
	
	@Transactional(rollbackFor=Exception.class)
	public void updateByCode(T bean,Object code) {
		this.getMapper().update(bean, new SqlWrapper().addCondition("code",code));
	}
	
	public T queryOneById(Object id) {
		return getMapper().queryOne(new SqlWrapper().addCondition("id",id));
	}
	
	public T queryOneByCode(Object code) {
		return getMapper().queryOne(new SqlWrapper().addCondition("code",code));
	}
	
	
}
